<?php
  function translate($text_to_translate,$src_language,$target_language,$referer)
  {
    // Prepare language pair for Google API
    $lang_pair = $src_language.'|'.$target_language;
    
    // URL for Google translate API with the query for translation
    $google_url = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=".urlencode($text_to_translate)."&langpair=".urlencode($lang_pair);
        
    
    // Initialize CURL and send request
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $google_url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_REFERER, $referer);
    $response = curl_exec($curl);
    curl_close($curl);
    
    // Process the JSON string
    $json_response = json_decode($response, true);
    
    // If it unsuccessful return
    if ($json_response['responseStatus'] != 200){
      return false;
    }
    
    // Collect the result
    $result = $json_response['responseData']['translatedText'];
    
    // Return the result
    return $result;
  }
  
  function translate_resx($input_resx_file_path, $output_resx_file_path, $src_language, $target_language, $referer)
  {
	// Parse the file
	$dom = new DomDocument();
	$dom->load($input_resx_file_path); 
	
	// Prepare XPath to collect the strigs
	$xp = new domxpath($dom);
	$titles = $xp->query("/root/data");
	
	// Some local variables for use
	$index=0;
	$token='';
	
	// Read RESX template
	$handleread = fopen("resx_template.resx", 'r');
	$read = fread($handleread,filesize("resx_template.resx"));
	
	//Open the output Resx file for writing
	$Handle = fopen($output_resx_file_path, 'w');
	
	// For each nodes, process the strings
	foreach ($titles as $node) {
		$text_to_translate = $node->nodeValue;	// Collect the node value
		$result = translate($text_to_translate, $src_language, $target_language, $referer);	//Translate the same				
		
		// If the result is not empty, prepare the output node
		if($result != NULL || $result != '')
		{
			$token .= '<data name="' . $node->getAttribute('name') . '" xml:space="preserve"><value>' . $result . '</value></data>';			
		}
		
		// Increment the loop variable
		$index++;
	}
	
	// Replace the newly formed translated nodes into the templated text
	$text_to_be_put = str_replace('DATA', $token, $read);
	
	
	// Write to output file
	fwrite($Handle, $text_to_be_put); 
	
	// Close the file handles
	fclose($Handle);
	fclose($handleread);
  }

  function translate_properties($input_prop_file_path, $output_prop_file_path, $src_language, $target_language, $referer)
  {
	 // Read .properties file
	$line_array = explode("\n", file_get_contents($input_prop_file_path));
	
	// Some useful variables
	$token = '';
	
	//Open the output .properties file for writing
	$Handle = fopen($output_prop_file_path, 'w');
	
	foreach($line_array as $line)
	{
		// Ignore the lines, which starts with '#', that's a comment
		if(substr($line,0,1) != '#')
		{
			$splitted_val = explode("=",$line);
			$key = $splitted_val[0];
			$value = $splitted_val[1];
			$result = translate($value, $src_language, $target_language, $referer);
			$token .= $key . ' = ' . $result . '\r\n';
			
			// Write to output file
			fwrite($Handle, $token); 
		}else
		{
			fwrite($Handle, $line); 
		}
	}
	
	// Close the file handles
	fclose($Handle);
  }
?>